<template>
  <div class="app-container">
    <!-- <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="部门名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入部门名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="部门状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form> -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button plain icon="el-icon-plus" size="mini" @click="handleAdd"
          >新增</el-button
        >
        <el-button plain size="mini" @click="handleSelectAll">全选</el-button>
        <el-button
          plain
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          type="danger"
          >删除</el-button
        >
        <el-button
          plain
          size="mini"
          :disabled="multiple"
          @click="handleUpdateStatus(1)"
          >隐藏</el-button
        >
        <el-button
          plain
          size="mini"
          :disabled="multiple"
          @click="handleUpdateStatus(0)"
          >显示</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      ref="table"
      v-loading="loading"
      :data="list"
      row-key="categoryId"
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column
        prop="categoryName"
        label="标题/名称"
        width="150"
      ></el-table-column>
      <el-table-column
        label="创建更新时间"
        align="center"
        prop="updateTime"
        width="150"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.updateTime || scope.row.createTime)
          }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="sort" label="排序" width="100"></el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.status == 0 ? "显示" : "隐藏" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="type"
        label="类型"
        width="100"
        :formatter="typeFormat"
      ></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            v-if="scope.row.level < 2"
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            >添加分类</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24" v-if="!!form.parentId">
            <el-form-item label="所属分类" prop="parentId">
              {{ form.parentName }}
              <!-- <treeselect
                v-model="form.parentId"
                :options="deptOptions"
                :normalizer="normalizer"
                placeholder="选择所属分类"
              /> -->
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="类别名称" prop="categoryName">
              <el-input
                v-model="form.categoryName"
                placeholder="请输入类别名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="类型" prop="type">
              <el-radio-group v-model="form.type">
                <el-radio
                  v-for="item in tpyeList"
                  :key="item.id"
                  :label="item.id"
                  >{{ item.name }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="排序" prop="sort">
              <el-input-number
                v-model="form.sort"
                controls-position="right"
                :min="0"
                :max="99"
                style="width:100%;"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPage,
  putObj,
  addObj,
  getObj,
  delObj,
  changeStatus
} from "@/api/cms/category";

import { setTreeLevel } from "@/utils/ruoyi";

export default {
  name: "Category",
  data() {
    return {
      tpyeList: [
        { id: 0, name: "新闻" },
        { id: 1, name: "下载" },
        { id: 2, name: "视频" },
        { id: 3, name: "考试" }
      ],
      // 遮罩层,
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        deptName: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        categoryName: [
          { required: true, message: "部门名称不能为空", trigger: "blur" }
        ],
        type: [{ required: true, message: "类型不能为空", trigger: "blur" }],
        sort: [{ required: true, message: "显示排序不能为空", trigger: "blur" }]
      },
      list: [],
      cList: [],
      ids: [],
      single: true,
      multiple: true
    };
  },
  filters: {},
  created() {
    this.getList();
  },
  methods: {
    typeFormat(row) {
      if (row.type == 0) {
        return "新闻";
      } else if (row.type == 1) {
        return "下载";
      } else if (row.type == 2) {
        return "视频";
      } else if (row.type == 3) {
        return "考试";
      }
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.categoryId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      getPage(this.queryParams).then(response => {
        this.cList = response.data;
        this.list = setTreeLevel(this.handleTree(response.data, "categoryId"));
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        categoryName: "",
        description: "",
        sort: 1,
        status: "0",
        parentName: "",
        parentId: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row != undefined) {
        this.form.parentId = row.categoryId;
        this.form.parentName = row.categoryName;
      }
      this.open = true;
      this.title = "添加类别";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getObj(row.categoryId).then(response => {
        this.form = response.data;
        this.form.parentId = row.parentId;
        const p = this.cList.find(item => item.categoryId == row.parentId);
        this.form.parentName = p ? p.categoryName : "";
        this.open = true;
        this.title = "修改类别";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.categoryId != undefined) {
            putObj(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addObj(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.id || this.ids;
      this.$confirm('是否确认删除编号为"' + Ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delObj(Ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    // 更新状态
    handleUpdateStatus(status) {
      changeStatus({
        ids: this.ids,
        status
      }).then(response => {
        this.msgSuccess(status == 1 ? "隐藏成功" : "显示成功");
        this.getList();
      });
    },
    handleSelectAll() {
      this.$refs.table.toggleAllSelection();
    }
  }
};
</script>
